package cn.zh.blog.controller;

import cn.zh.blog.pojo.Blog;
import cn.zh.blog.pojo.Tag;
import cn.zh.blog.service.BlogService;
import cn.zh.blog.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

/**
 * @Author 郑豪
 * @Date 2020/4/8 21:30
 **/
@Controller
public class TagShowController {

    @Autowired
    private TagService tagService;
    @Autowired
    private BlogService blogService;

    @GetMapping("/tags/{id}")
    public String tags(@PageableDefault(size = 3,sort = {"updateTime"},direction = Sort.Direction.DESC) Pageable pageable,
                       Model model, @PathVariable Long id) {
        List<Tag> tags = tagService.listTagTop(1000);
        if (id == -1) {
            if (tags.size() == 0){
                model.addAttribute("message","没有博文标签，请添加-_-");
            }else {
                id = tags.get(0).getId();
            }
        }
        Page<Blog> page = blogService.listBlog(id, pageable);
        if (page.isEmpty()) {
            model.addAttribute("isTagNull","该标签下未查询到博客文章-_-!");
        }
        model.addAttribute("tags",tags);
        model.addAttribute("page",page);
        model.addAttribute("activeTagId",id);
        return "tags";
    }
}
